
#include <iostream>

#include "jell/netjell.hpp"

#include "jell/jell_address.hpp"

#include "jell/buf_handler.hpp"

using namespace jell;
using namespace std;

class   EchoHandler : public BufHandler
{
public  :
    virtual ~EchoHandler() {}

    /**
     * @return <0 -- failure and continue,
     *         =0 -- success and continue,
     *         >0 -- success and stop
     */
    virtual int     handleBuf(Buf& buf, const uri_type& uri, Context& context) throw()
    {
        try {
            int ret = context.send(buf, uri);
        }
        catch(JellException& e) {
            cerr << "EchoHandler Exception :" << e << endl;
        }

        return  0;
    }

};

int main(int argc, char* argv[])
{
    const char* pszAddress = "udp://127.0.0.1:4344";
    JellAddress address( pszAddress );

    // initialize
    NetJell*    jell_p = NetJell::create();
    try {
        jell_p->listen( address );
    }
    catch(JellException& e) {
        cerr << e << endl;
    }

    EchoHandler  echo_buf_handler;
    try {
        jell_p->addBufHandler( echo_buf_handler );
    }
    catch(JellException& e) {
        cerr << e << endl;
    }

    // run
    try {
        jell_p->run();
    }
    catch(JellException& e) {
        cerr << e << endl;
    }

    // finalize
    try {
        jell_p->shutdown( address );
    }
    catch(JellException& e) {
        cerr << e << endl;
    }

    delete jell_p;

    return  0;
}
